import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;

class UniversalDialog {
  static void show({
    required BuildContext context,
    required String title,
    required String content,
    bool barrierDismissible = true,
    String confirmText = "确定",
    String cancelText = "取消",
    VoidCallback? onConfirm,
    VoidCallback? onCancel,
  }) {
    if (kIsWeb || !isDesktop()) {
      // Web & Mobile 端默认对话框
      showDialog(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) => AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                onCancel?.call();
              },
              child: Text(cancelText),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                onConfirm?.call();
              },
              child: Text(confirmText),
            ),
          ],
        ),
      );
    } else {
      // 桌面端 - 自定义可拖拽弹框
      showDialog(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) => _DraggableDialog(
          title: title,
          content: content,
          confirmText: confirmText,
          cancelText: cancelText,
          onConfirm: onConfirm,
          onCancel: onCancel,
        ),
      );
    }
  }

  static bool isDesktop() {
    return [
      TargetPlatform.macOS,
      TargetPlatform.windows,
      TargetPlatform.linux,
    ].contains(defaultTargetPlatform);
  }
}

/// 桌面端可拖拽弹框
class _DraggableDialog extends StatefulWidget {
  final String title;
  final String content;
  final String confirmText;
  final String cancelText;
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;

  const _DraggableDialog({
    required this.title,
    required this.content,
    required this.confirmText,
    required this.cancelText,
    this.onConfirm,
    this.onCancel,
  });

  @override
  _DraggableDialogState createState() => _DraggableDialogState();
}

class _DraggableDialogState extends State<_DraggableDialog> {
  Offset offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        GestureDetector(
          onTap: () => Navigator.pop(context),
          child: Container(color: Colors.black54),
        ),
        Positioned(
          left: MediaQuery.of(context).size.width / 2 - 150 + offset.dx,
          top: MediaQuery.of(context).size.height / 2 - 100 + offset.dy,
          child: GestureDetector(
            onPanUpdate: (details) {
              setState(() => offset += details.delta);
            },
            child: Material(
              color: Theme.of(context).dialogBackgroundColor,
              borderRadius: BorderRadius.circular(8),
              elevation: 4,
              child: Container(
                width: 300,
                padding: const EdgeInsets.all(16),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(widget.title,
                        style: const TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    const SizedBox(height: 10),
                    Text(widget.content, textAlign: TextAlign.center),
                    const SizedBox(height: 20),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        TextButton(
                          onPressed: () {
                            Navigator.pop(context);
                            widget.onCancel?.call();
                          },
                          child: Text(widget.cancelText),
                        ),
                        TextButton(
                          onPressed: () {
                            Navigator.pop(context);
                            widget.onConfirm?.call();
                          },
                          child: Text(widget.confirmText),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
